package com.solocredit.finance.ui.user.impl

import android.content.Context
import android.graphics.Color
import com.solocredit.finance.R
import com.solocredit.finance.app.MyApp
import com.solocredit.finance.http.IHttpCallback
import com.solocredit.finance.mvp.presenter.MvpPresenter
import com.solocredit.finance.ui.user.view.IMullaUserVipView
import com.solocredit.finance.utils.DateUtils
import com.solocredit.finance.utils.helper.MullaCustomConfigsHelper
import com.solocredit.finance.utils.helper.MullaCustomDataHelper
import com.solocredit.finance.utils.helper.MullaUserPointsHelper
import com.solocredit.finance.weight.banner.entity.UserVipInfo
import org.json.JSONObject

/**
 * MullaFlow
 * @Author wwdeng
 * @CreateTime: 2024/7/26 16:26
 * @Annotation：用户Vip Presenter 实现
 */
class MullaUserVipPresenterImpl(context: Context?) : MvpPresenter<IMullaUserVipView>(context) {

    var vipGradeList: ArrayList<UserVipInfo>? = null

    /**
     * 加载Vip列表
     */
    fun loadMullaVipGradeListAction(){
        if (vipGradeList == null){
            vipGradeList = arrayListOf()
        }
        vipGradeList?.clear()
        val vip1Item = UserVipInfo()
        vip1Item.vip = 1
        vip1Item.backgroundResId = R.mipmap.mulla_user_vip1__bg
        vip1Item.bannerResId = R.mipmap.mulla_user_vip1_banner_bg
        vip1Item.colorId = Color.parseColor("#FF798197")
        vip1Item.moreIcon = R.mipmap.mulla_user_vip1_more_icon
        vip1Item.vipLable = R.mipmap.mulla_user_vip1_lable_icon
        vip1Item.vipText = R.mipmap.mulla_user_vip1_text
        vip1Item.vipDoubt = R.mipmap.mulla_user_vip1_doubt_icon
        vip1Item.vGrade = "V1"
        val vip1Obj = JSONObject()
        vip1Obj.put("discountIcon",R.mipmap.mulla_user_vip1_benefits_discount)
        vip1Obj.put("discountUnlock",true)
        vip1Obj.put("discountDes",R.string.vip_exchange_discount_not_enough)
        vip1Obj.put("titleColor",Color.parseColor("#FFB4BADA"))
        vip1Obj.put("desColor",Color.parseColor("#FF9599BA"))

        vip1Obj.put("repaymentIcon",R.mipmap.mulla_user_vip1_benefits_repayment)
        vip1Obj.put("repaymentUnlock",true)
        vip1Obj.put("repaymentDes",R.string.vip_points_bonus_not_enough)

        vip1Obj.put("birthdayIcon",R.mipmap.mulla_user_vip1_benefits_birthday)
//        vip1Obj.put("birthdayDes",R.string.vip_birthday_tips)

        vip1Obj.put("otherIcon",R.mipmap.mulla_user_vip1_benefits_other)
        vip1Obj.put("otherDes",R.string.vip_tips)
        vip1Item.vipBenefits = vip1Obj
        vipGradeList?.add(vip1Item)

        val vip2Item = UserVipInfo()
        vip2Item.vip = 2
        vip2Item.backgroundResId = R.mipmap.mulla_user_vip2__bg
        vip2Item.bannerResId = R.mipmap.mulla_user_vip2_banner_bg
        vip2Item.colorId = Color.parseColor("#FFB06D25")
        vip2Item.moreIcon = R.mipmap.mulla_user_vip2_more_icon
        vip2Item.vipLable = R.mipmap.mulla_user_vip2_lable_icon
        vip2Item.vipText = R.mipmap.mulla_user_vip2_text
        vip2Item.vipDoubt = R.mipmap.mulla_user_vip2_doubt_icon
        vip2Item.vGrade = "V2"
        val vip2Obj = JSONObject()
        vip2Obj.put("discountIcon",R.mipmap.mulla_user_vip2_benefits_discount)
        vip2Obj.put("discountUnlock",false)
        vip2Obj.put("discountDes",R.string.vip_exchange_discount_tips)
        vip2Obj.put("titleColor",Color.parseColor("#FF7F8386"))
        vip2Obj.put("desColor",Color.parseColor("#FFFFC971"))

        vip2Obj.put("repaymentIcon",R.mipmap.mulla_user_vip2_benefits_repayment)
        vip2Obj.put("repaymentUnlock",false)
        vip2Obj.put("repaymentDes",R.string.vip_points_bonus_tips)

        vip2Obj.put("birthdayIcon",R.mipmap.mulla_user_vip2_benefits_birthday)
//        vip2Obj.put("birthdayDes",R.string.vip_birthday_receive_wait)

        vip2Obj.put("otherIcon",R.mipmap.mulla_user_vip2_benefits_other)
        vip2Obj.put("otherDes",R.string.vip_tips)
        vip2Item.vipBenefits = vip2Obj
        vipGradeList?.add(vip2Item)

        val vip3Item = UserVipInfo()
        vip3Item.vip = 3
        vip3Item.backgroundResId = R.mipmap.mulla_user_vip3__bg
        vip3Item.bannerResId = R.mipmap.mulla_user_vip3_banner_bg
        vip3Item.colorId = Color.parseColor("#FF391C53")
        vip3Item.moreIcon = R.mipmap.mulla_user_vip3_more_icon
        vip3Item.vipLable = R.mipmap.mulla_user_vip3_lable_icon
        vip3Item.vipText = R.mipmap.mulla_user_vip3_text
        vip3Item.vipDoubt = R.mipmap.mulla_user_vip3_doubt_icon
        vip3Item.vGrade = "V3"
        val vip3Obj = JSONObject()
        vip3Obj.put("discountIcon",R.mipmap.mulla_user_vip3_benefits_discount)
        vip3Obj.put("discountUnlock",false)
        vip3Obj.put("discountDes",R.string.vip_exchange_discount_tips)
        vip3Obj.put("titleColor",Color.parseColor("#FF7F8386"))
        vip3Obj.put("desColor",Color.parseColor("#FFFFC971"))

        vip3Obj.put("repaymentIcon",R.mipmap.mulla_user_vip3_benefits_repayment)
        vip3Obj.put("repaymentUnlock",false)
        vip3Obj.put("repaymentDes",R.string.vip_points_bonus_tips)

        vip3Obj.put("birthdayIcon",R.mipmap.mulla_user_vip3_benefits_birthday)
//        vip3Obj.put("birthdayDes",R.string.vip_birthday_received)

        vip3Obj.put("otherIcon",R.mipmap.mulla_user_vip3_benefits_other)
        vip3Obj.put("otherDes",R.string.vip_tips)
        vip3Item.vipBenefits = vip3Obj
        vipGradeList?.add(vip3Item)

        vipGradeList?.let {
            mvpView?.showMullaVipGradeList(it)
        }
    }

    /**
     * 设置生日福利状态
     * 月份相同 && 本年未领取
     *
     * -1: 未设置生日
     * 0 ：待领取
     * 1 ：已领取
     * 2 ：不可领取
     */
    fun loadMullaUserBirthdayBenefitsStatus(){
        //生日（时间戳 秒）
        val birthday = MullaCustomDataHelper.getMullaCustomUserBirthday()
        if (birthday.isEmpty()){
            //未设置生日 - 点击跳转到 “个人简介” 页面
            mvpView?.showMullaUserBirthdayBenefitsView(-1)
            return
        }
        //生日年月
//        val birthdayYear = DateUtils.getFormatTime(birthday.toLong() * 1000,"yyyy")
        val birthdayMonth = DateUtils.getFormatTime(birthday.toLong() * 1000,"MM")

        //当前年月
        val curtYear = DateUtils.getFormatTime(System.currentTimeMillis(),"yyyy")
        val curtMonth = DateUtils.getFormatTime(System.currentTimeMillis(),"MM")

        //生日福利领取时间（时间戳 秒）
        val benefitsReceiveTime = MullaCustomDataHelper.getMullaCustomBirthdayReceiveTimestamp()
        if (benefitsReceiveTime.isEmpty()){
            //未领取 && 当前月 == 生日月 --> 待领取
            if (birthdayMonth == curtMonth){
                mvpView?.showMullaUserBirthdayBenefitsView(0)
            } else {
                //未领取 && 当前月 != 生日月 --> 不可领取
                mvpView?.showMullaUserBirthdayBenefitsView(2)
            }
            return
        }

        //领取年月
        val receiveYear = DateUtils.getFormatTime(benefitsReceiveTime.toLong() * 1000,"yyyy")
//        val receiveMonth = DateUtils.getFormatTime(benefitsReceiveTime.toLong() * 1000,"MM")

        //有领取时间戳
        if (curtYear == receiveYear){
            //有领取时间戳 && 当前年 == 领取年 -->  已领取
            mvpView?.showMullaUserBirthdayBenefitsView(1)
            return
        }

        //有领取时间戳 && 当前年 != 领取年
        if (birthdayMonth == curtMonth){
            //有领取时间戳 && 当前年 != 领取年 && 生日月 == 当前月  --> 待领取
            mvpView?.showMullaUserBirthdayBenefitsView(0)
            return
        }

        //有领取时间戳 && 当前年 != 领取年 && 生日月 != 当前月  --> 不可领取
        mvpView?.showMullaUserBirthdayBenefitsView(2)
    }

    /**
     * 保存生日福利领取记录
     */
    fun saveMullaUserBirthdayBenefitsReceiveTimeAction(){
        MullaCustomDataHelper.saveMullaCustomUserBirthdayBenefitsReceiveTime("${System.currentTimeMillis() / 1000}",object : IHttpCallback {
            override fun onRequestSuccess(data: JSONObject) {
                when(data.optInt("code")) {
                    0 -> {//成功
                        saveMullsUserBirthdayBenefitsPointsAction()
                    }
                    else -> {
                        mvpView?.showMullaToast(data.optString("msg"))
                        mvpView?.hideMullaLoading()
                    }
                }
            }

            override fun onRequestError(error: JSONObject) {
                mvpView?.hideMullaLoading()
            }

            override fun onRequestFinish() {
            }
        })
    }

    /**
     * 增加积分
     */
    fun saveMullsUserBirthdayBenefitsPointsAction(){
        val addPoints = MullaCustomConfigsHelper.getMullaBirthdayBenefitsPoints()
        MullaUserPointsHelper.saveMullsUserPointsAction("birthdayBenefitsReceive",MyApp.app.getString(R.string.points_use_info_birthday_benefits),addPoints,object : IHttpCallback{
            override fun onRequestSuccess(data: JSONObject) {
                when(data.optInt("code")) {
                    0 -> {//成功
                        mvpView?.showMullaUserBirthdayBenefitsReceiveSuccess(addPoints)
                    }
                    else -> {
                        mvpView?.showMullaToast(data.optString("msg"))
                    }
                }
            }

            override fun onRequestError(error: JSONObject) {
                mvpView?.showMullaToast(error.optString("errorMsg"))
            }

            override fun onRequestFinish() {
                mvpView?.hideMullaLoading()
            }
        })
    }
}